import ChatStream from "./chatstream"
import useTokenStore from "@/stores/token"
import request, { wrap_api_func } from "./request"

const BASE_URL = "/api/v1/chat"

export const aiChater = new ChatStream(`${BASE_URL}/ai/completions`)

export const list_chat_dialogs = wrap_api_func(async ({ assistantId }) => {
    const { data } = await request.get(`${BASE_URL}/${assistantId}/dialogs/list`, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: {
            dialogs: data.data ? data.data.dialogs : []
        }
    }
})

export const rename_chat_dialog = wrap_api_func(async ({ chatDialog, newName, requestId }) => {
    const { data } = await request.post(`${BASE_URL}/dialog/rename`, {
        id: chatDialog.id,
		name: newName,
		lastActiveAt: chatDialog.lastActiveAt,
		assistantId: chatDialog.assistantId,
		requestId: requestId,
    }, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: data.data // chat dialog
    }
})

export const delete_chat_dialog = wrap_api_func(async ({ dialogId }) => {
    const { data } = await request.post(`${BASE_URL}/dialog/${dialogId}/delete`, null, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: data.data // chat dialog
    }
})

export const list_chat_msgs = wrap_api_func(async ({ dialogId }) => {
    const { data } = await request.get(`${BASE_URL}/${dialogId}/msgs/list`, {
        headers: useTokenStore().getAuthHeader()
    })
    return {
        code: data.code,
        msg: data.msg,
        data: {
            msgs: data.data ? data.data.msgs : []
        }
    }
})